import sys
import os
# from PyQt5 import QtCore, QtGui, QtWidgets, Qt
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow,QFileDialog,QMessageBox
# from PyQt5.QtWidgets import *
# from PyQt5.QtCore import *
from app.uis.Ui_window import Ui_MainWindow
from Install import Install
from Db import Db
import webbrowser
# 导入所有页面views
from app.views.YanjiaSetting import YanjiaSetting
from app.views.Category import Category
from app.views.Project import Project
# 添加环境变量------start
pwd = os.getcwd()
path = os.environ['path']
if(pwd in path):
    print(True)
else:
    os.environ["path"] += os.pathsep + pwd + ";"  #注意修改你的路径
path = os.environ['path']
# 添加环境变量------end

class MyWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)
        self.setupUi(self)
        # 安装数据表------start
        install = Install()
        install.run()
        # 安装数据表------end

        # 显示项目
        self.dataShow()
        
        # 配置弹窗
        self.actionasd.triggered.connect(self.YanjiaSettingShow)
        self.actionfenlei.triggered.connect(self.CategoryShow)
        self.actionasda.triggered.connect(self.ProjectShow)
        self.pushButton_6.clicked.connect(self.ProjectShow)

        self.actionasd_2.triggered.connect(self.tourl)
    def tourl(self):
        url="http://www.baidu.com"
        try:
            webbrowser.get('chrome').open_new_tab(url)
        except Exception as e:
            webbrowser.open_new_tab(url)

    def YanjiaSettingShow(self):
        self.t = YanjiaSetting()
        self.t.show()
    def CategoryShow(self):
        self.c = Category()
        self.c.show()
    def ProjectShow(self):
        self.c = Project()
        self.c.signal_updateDefault.connect(self.dataShow)
        self.c.show()
    def dataShow(self):
        self.db = Db()
        sql = '''
            select * from projects where `is_default` = '当前选择'
        '''
        project_name = '当前无默认项目'
        data = self.db.queryReturnDict(sql)
        for v in data:
            project_name = v['name']
        self.label_7.setText(project_name)
    # -------------------------------全局-------------------------------------
    def closeEvent(self, event):
        reply = QMessageBox.information(self,
                                    "提示",  
                                    "是否确定退出?",  
                                    QMessageBox.Yes | QMessageBox.No)
        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()    

# pyinstaller -F -w -i 2.ico main.py (主程序界面)
if __name__ == '__main__': 
    app = QApplication(sys.argv)
    myWin = MyWindow()
    myWin.show()
    sys.exit(app.exec_())